﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Drawing;
using DevExpress.Web.ASPxPivotGrid;


public partial class WebUserControl_uscBaoCaoDHTungTVV : System.Web.UI.UserControl
{
    string error = "";
    static DataTable dataCap;
    ClassExecute.ClassExecute dt;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

        }
        else
        {
            try
            {
                Title();
                this.ASPxPivotGrid1.DataSource = dataCap;
                this.ASPxPivotGrid1.DataBind();
            }
            catch
            {
                return;
            }
        }
    }
    public void Title()
    {
        if (!ASPxPivotGrid1.Fields.Contains(ASPxPivotGrid1.Fields["Baocao"]))
        {
            ASPxPivotGrid1.Fields.Add("Baocao", DevExpress.XtraPivotGrid.PivotArea.FilterArea);
            ASPxPivotGrid1.Fields["Baocao"].HeaderStyle.BackColor = Color.Yellow;
            ASPxPivotGrid1.Fields["Baocao"].Caption = "Báo Cáo Danh Hiệu Từng Tư Vấn Viên Trong Năm";
        }
        if (!ASPxPivotGrid1.Fields.Contains(ASPxPivotGrid1.Fields["Time"]))
        {
            ASPxPivotGrid1.Fields.Add("Time", DevExpress.XtraPivotGrid.PivotArea.FilterArea);
            ASPxPivotGrid1.Fields["Time"].HeaderStyle.BackColor = Color.Yellow;
            ASPxPivotGrid1.Fields["Time"].Caption = "Ngày giờ lấy báo cáo : " + DateTime.Now.ToShortDateString() + "  " + DateTime.Now.ToShortTimeString();
        }
    }
    private void ViewReport()
    {
        try
        {

            ASPxPivotGrid1.Fields.Clear();
            Title();
            dt = new ClassExecute.ClassExecute();
            dataCap = dt.Get_Data_DataTable(str, ref error, "Tr_BaoCaoDanhHieuTungTVV", CommandType.StoredProcedure, new SqlParameter("@UserName", Session["username"].ToString()), new SqlParameter("@Salesp_Key", TextBox1.Text.Trim()));
            foreach (DataColumn col in dataCap.Columns)
            {

                ASPxPivotGrid1.Fields.Add(col.ColumnName.ToString(), DevExpress.XtraPivotGrid.PivotArea.RowArea);

            }

            this.ASPxPivotGrid1.DataSource = dataCap;
            this.ASPxPivotGrid1.DataBind();
        }
       
        catch (Exception ex)
        {
            uscMsgBox2.AddMessage("Không có dữ liệu" + "\n" + error + "\n" + ex, uscMsgBox.enmMessageType.Error);
        }
    }
   
    protected void btnXemBaoCao_Click(object sender, EventArgs e)
    {
        ViewReport();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        using (MemoryStream filestream = new MemoryStream())
        {

            string contentType = "application/ms-excel";
            string fileName = System.DateTime.Now.ToString("dd/MM/yyyy") + "_BaoCaoDanhHieuTungTVVTrongNam.xls";
            ASPxPivotGridExporter1.ExportToXls(filestream);
            byte[] buffer = filestream.GetBuffer();
            Response.Clear();
            Response.Buffer = false;
            Response.AppendHeader("Content-Type", contentType);
            Response.AppendHeader("Content-Transfer-Encoding", "binary");
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
            Response.BinaryWrite(buffer);
            Response.End();
        }
    }
}
